package com.hava.web.spring;

import com.google.common.collect.Maps;
import com.hava.demo.springmvc.onlyone.entity.OnlyOne;
import com.hava.demo.springmvc.onlyone.service.OnlyOneService;
import com.hava.web.MediaType;
import com.hava.web.rest.Entity;
import com.hava.web.serlvet.QueryRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.UriComponentsBuilder;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.net.URI;
import java.util.List;
import java.util.Map;

/**
 * Created by zhanpeng on 15-2-26.
 * 一般意义上的web只使用GET和POST的HTTP函数，而新的HTTP函数还具有其他，必要在其他的地方进行使用
 * OnlyOne管理的Controller, 使用Restful风格的Urls:
 *
 * List page : GET /onlyone/
 * Create page : GET /onlyone/create
 * Create action : POST /onlyone/create
 * Update page : GET /onlyone/update/{id}
 * Update action : POST /onlyone/update
 * Delete action : GET /onlyone/delete/{id}
 */

//@Controller
//声明把OnlyOneController映射到/demo/onlyone
//@RequestMapping(value = "/demo/onlyone")
public class RestController <ENTITY>{

    @Autowired
    private OnlyOneService onlyOneService;

    @Autowired
    //private Validation validation;

    //GET /entity : get entity all list
    //注意：这里与springside不同的地方是选择使用google common MediaType
    @RequestMapping(method = RequestMethod.GET,produces = MediaType.JSON)
    public List<OnlyOne> list(){
        return onlyOneService.getAllOnlyOne();
    }

    //GET /entity/{id} : get entity.id={id} object
    @RequestMapping(method = RequestMethod.GET,value = "/{id}",produces = MediaType.JSON)
    public OnlyOne get(@PathVariable("id") Long id){

        OnlyOne onlyOne = onlyOneService.getOnlyOne(id);

        if(onlyOne == null)
        {
            String message = "OnlyOne Object :id {" + id + "} not existed." ;
            System.out.println(message);
        }

        return onlyOne;
    }

    //POST /entity : create new entity object
    @RequestMapping(method = RequestMethod.POST,consumes = MediaType.JSON)
    public ResponseEntity<?> create(@RequestBody OnlyOne onlyOne,UriComponentsBuilder uriBuilder){

        System.out.println("create");
        //System.out.println("input:" + test);
        //TODO:JSR303校验

        onlyOneService.createOnlyOne(onlyOne);

        //Long id = onlyOne.getId();

        //按照Restful风格，返回新对象的
        HttpHeaders httpheader = new HttpHeaders();
        String entity_url = null;

        //通过反射获取entity的url
        String EntityURL = Entity.URL(this);

        URI uri = uriBuilder.path("").build().toUri();
        httpheader.setLocation(uri);

        ResponseEntity entity = new ResponseEntity(httpheader, HttpStatus.CREATED);

        return entity;
    }

    //PUT /entity/{id} : update entity.id={id}
    @RequestMapping(method = RequestMethod.PUT,value = "/{id}",consumes = MediaType.JSON)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void update(@RequestBody OnlyOne onlyOne,@PathVariable("id") Long id)
    {
        onlyOne.setId(id);
        onlyOneService.updateOnlyOne(onlyOne);
    }

    //DELETE /entity/{id} : delete entity.id={id}
    @RequestMapping(method = RequestMethod.DELETE,value = "/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable("id") Long id){
        onlyOneService.deleteOnlyOne(id);
    }
}